package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//服务器配置

//新增服务器配置
func NewServer(data map[string]interface{}) (int64, error) {
	o := NewDBOrm(DBChessSysData)
	values, sql := InsertSql(TableServerInfo, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//更新服务器配置
func UpdateServer(rc map[string]interface{}, svrId int64) error {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf(`svrid = %v`, svrId)
	values, sql := UpdateSql(TableServerInfo, rc, condition)
	_, err := o.Raw(sql, values).Exec()
	return err
}

//删除服务器配置
func DeleteServer(svrId int64) (int64, error) {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf("svrid=%v", svrId)
	sql := DeleteSql(TableServerInfo, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//查看服务器信息
func ServerInfoById(svrId int64) (*Server, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			svrid = %v
	`, TableServerInfo, svrId)
	var gl Server
	o := NewDBOrm(DBChessSysData)
	err := o.Raw(sql).QueryRow(&gl)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &gl, err
}

//房间服务器配置列表
func ListServer(p ListParams) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	o := NewDBOrm(DBChessSysData)
	sql := fmt.Sprintf("SELECT * FROM %v ORDER BY `group`,svr_type  LIMIT ?,?", TableServerInfo)
	var rc []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&rc)
	if err != nil {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableServerInfo)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return rc, count, err
	}
	return rc, count, nil
}

//---------------------------------------平台服务器-------------------------------
//新增服务器配置
func NewPlatform(data map[string]interface{}) (int64, error) {
	o := NewDBOrm(DBChessSysData)
	values, sql := InsertSql(TablePlatform, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//更新服务器配置
func UpdatePlatform(rc map[string]interface{}, id int64) error {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TablePlatform, rc, condition)
	_, err := o.Raw(sql, values).Exec()
	return err
}

//删除服务器配置
func DeletePlatform(id int64) (int64, error) {
	o := NewDBOrm(DBChessSysData)
	condition := fmt.Sprintf("id =%v", id)
	sql := DeleteSql(TablePlatform, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//查看服务器信息
func PlatformInfoById(id int64) (*Platform, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = %v
	`, TablePlatform, id)
	var gl Platform
	o := NewDBOrm(DBChessSysData)
	err := o.Raw(sql).QueryRow(&gl)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &gl, err
}

//房间服务器配置列表
func ListPlatform(p ListParams) ([]*Platform, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	o := NewDBOrm(DBChessSysData)
	sql := fmt.Sprintf(`
			SELECT
				*
			FROM
				%v
			LIMIT ?,?
		`, TablePlatform)
	var rc []*Platform
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&rc)
	if err != nil {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TablePlatform)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return rc, count, err
	}
	return rc, count, nil
}
